﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using IniFiles;
using Instruments;

namespace Cali
{
    public partial class Form_Main : Form
    {
        public static string m_PathGeneral;
        public static Calibration AC_Cali;
        public static IniConfig m_Ininfo;
        public Form_Ins form_Ins;
        public static string sLog1;
        public static string sLog2;
        public Form_Main()
        {
            InitializeComponent();
            m_PathGeneral = System.IO.Path.Combine(Application.StartupPath, "General.ini");

        }

        private void Form_Main_Load(object sender, EventArgs e)
        {
            Form_FilePicker form = new Form_FilePicker();
            IniFile iniFile;
            string iniPath, buff;

            //检测ini配置档，并读取。
            if (!File.Exists(m_PathGeneral))
            {
                if (MessageBox.Show("配置文件：" + m_PathGeneral+"缺失，现在创建？", "Error", MessageBoxButtons.OKCancel)==DialogResult.OK)
                {
                    form.ShowDialog();  //完成.ini文件的查找
                }
            }

            iniFile = new IniFile(m_PathGeneral);
            iniPath = iniFile.ReadString("Settings", "iniPath", string.Empty);
            if (!File.Exists(iniPath))
                return;
            m_Ininfo = new IniConfig(iniPath);
            m_Ininfo.ReadGerneralConfig();
            AC_Cali = new Calibration();
            if (AC_Cali.i2C.Init() < 0)
                this.Text = m_Ininfo._sTypeI2CIO + " init Failed!";
            else
                this.Text = m_Ininfo._sTypeI2CIO + " init Success!";
            buff = AC_Cali.InstrumentsInit();
            if (!buff.Equals(string.Empty))
                MessageBox.Show(buff);

            InitGUI();
            form_Ins = new Form_Ins();

            m_Ininfo.ReadCALConfig();
        }

        /**
         * 界面初始化
         */
        private void InitGUI()
        {
            //菜单栏初始化
            mCPToolStripMenuItem.Checked = m_Ininfo._sTypeI2CIO.Equals("MCP");
            nI8451ToolStripMenuItem.Checked = m_Ininfo._sTypeI2CIO.Equals("NI8451");

            onePSUOnlyToolStripMenuItem.Checked = !m_Ininfo._bDual;
            autoTestEnToolStripMenuItem.Checked = m_Ininfo._bAutoTestEn;
            readSNFromEEPToolStripMenuItem.Checked = m_Ininfo._bReadSN;

            //界面初始化
            lblModelName.Text = m_Ininfo._sModelName;

            for (int i = 0; i < m_Ininfo._sTestNames.Length; i++)
            {
                dgvTestItems.Rows.Add();
                dgvTestItems.Rows[i].Cells[0].Value = (i + 1).ToString();
                dgvTestItems.Rows[i].Cells[1].Value = m_Ininfo._sTestNames[i];
            }
            
            dgvTestItems.AllowUserToAddRows = false;

            btnSingleTest.Visible = false;

            PSU1Log.Text = "ces\nce2";

            if(m_Ininfo._bDual)
            {
                //设置文本框边
            }
            else
            {
                dgvTestItems.Columns[3].Visible = false;

                PSU2Log.Visible = false;
                PSU1Log.Width = this.Width - 15;
                PSU1Log.ReadOnly = true;
            }

            txbStart.Text = DateTime.Now.ToString("T");
            txbEnd.Text = DateTime.Now.ToString("T");
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            //Calibration.Sources[0].Set_VFC("220", "50.0");
            //AC_Cali.Cali_SetLoad(0);
            //AC_Cali.Cali_GetOutputMes(0);
            //AC_Cali.Cali_GetInputMes(0);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 单步调试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using(Form_Password formPwd = new Form_Password(m_Ininfo._sPassword))
            {
                if(formPwd.ShowDialog().Equals(DialogResult.OK))
                {
                    单步运行ToolStripMenuItem.Checked = !单步运行ToolStripMenuItem.Checked;
                    btnSingleTest.Visible = 单步运行ToolStripMenuItem.Checked;
                }
            }
        }
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (Form_About form = new Form_About())
            {
                FileInfo fileInfo = new FileInfo(m_Ininfo._sPath);
                form.lblFileName.Text = fileInfo.Name;
                form.lblBomNumber99.Text = m_Ininfo._sBomNumber;
                form.lblNumber.Text = m_Ininfo._sCaliBom;
                form.lblDateCompile.Text = "2021-06-17";
                form.ShowDialog();
            }
        }

        private void onePSUOnlyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            onePSUOnlyToolStripMenuItem.Checked = !onePSUOnlyToolStripMenuItem.Checked;
            m_Ininfo._bDual = !onePSUOnlyToolStripMenuItem.Checked;
            dgvTestItems.Columns[3].Visible = m_Ininfo._bDual;
            PSU2Log.Visible = m_Ininfo._bDual;

            if (m_Ininfo._bDual)
            {
                PSU1Log.Width = this.Width / 2 - 10;
                PSU2Log.Width = this.Width / 2 - 10;
            }
            else
            {
                PSU1Log.Width = this.Width - 15;
            }
        }

        private void customToolStripMenuItem_Click(object sender, EventArgs e)
        {
            form_Ins.ShowDialog();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (Form_FilePicker form = new Form_FilePicker())
                form.ShowDialog();
        }

        private void btnSingleTest_Click(object sender, EventArgs e)
        {
            
        }

        private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_Ininfo.SaveGeneralConfig();
        }

        private void mCPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mCPToolStripMenuItem.Checked = nI8451ToolStripMenuItem.Checked ? true : false;
            nI8451ToolStripMenuItem.Checked = nI8451ToolStripMenuItem.Checked? false : true;
            m_Ininfo._sTypeI2CIO = mCPToolStripMenuItem.Checked ? "MCP" : "NI8451";
        }

        private void nI8451ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mCPToolStripMenuItem.Checked = nI8451ToolStripMenuItem.Checked ? true : false;
            nI8451ToolStripMenuItem.Checked = nI8451ToolStripMenuItem.Checked ? false : true;
            m_Ininfo._sTypeI2CIO = mCPToolStripMenuItem.Checked ? "MCP" : "NI8451";
        }
    }
}
